//////////////////////////////////////////////////////////////////////////
// FileName:Frame
// CreateBy:DragonXiong
// CreateDate:2009-5-4
// Description:
//		Class Frame
//      
// Modify Log:090612:Add Access Check --dragon
//            090623:Add function  Online Check  --Mark    
//            20100104:Using the AccessCheck method now, and no check userloginlog and personalsetting page.--DragonXiong
//                     Using NonAccess.aspx page when have no permission. --DragonXiong
//            20100105:Using Exists(string url, string sysID) in BLL SysFunctions class to check it's a menu or not --DragonXiong temp using this method
//	
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using System.Web;
using Auto.Components;
using Auto.Data.Setting;
using Auto.Web.Common;
using BLL = Auto.Data.Systems.BLL;

namespace BaseSystem
{
    public partial class Frame : System.Web.UI.MasterPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            Page.UICulture = "zh-CN";
            //if (Auto.Commons.SessionBox.User==null || !OnlineCheck() )
            //{

            //    Response.Redirect(Request.ApplicationPath + "/Timeout.aspx");  
            //}
            //if(!IsPostBack)
            //{
            // // UserDescription.Text = (Auto.Commons.SessionBox.User).UserID + ":" + (Auto.Commons.SessionBox.User).UserName;
            //}

            //AccessCheck();
            string appPath = Request.ApplicationPath;
            if (Auto.Web.Common.SessionBox.User == null || !OnlineCheck())
            {
                if (appPath.Length > 1)
                {
                    Response.Redirect(appPath + "/Timeout.aspx");
                }
                else
                {
                    Jscript.Redirect("/TimeOut.aspx");

                }

            }
            AccessCheck();

        }

        /// <summary>
        /// Access Check
        /// </summary>
        private void AccessCheck()
        {
            //try
            //{
            //    string[] fp = Request.FilePath.Split(new char[1] { '/' }, 3);

            //    string qs = "";
            //    //IEnumerator im = Request.QueryString.GetEnumerator();
            //    //if (im.MoveNext())
            //    //{
            //    //    qs = im.Current.ToString() + "=" + Request.QueryString[im.Current.ToString()].ToString();
            //    //    while (im.MoveNext())
            //    //    {
            //    //        qs = qs + "&" + im.Current.ToString() + "=" + Request.QueryString[im.Current.ToString()].ToString(); ;
            //    //    }

            //    //    if (qs != "")
            //    //        qs = "?" + qs;
            //    //}
            //    //AccessRight ar = new AccessRight();
            //    //bool access = ar.CheckAccess(Session["UserID"].ToString(), Session["UserGroupID"].ToString(), fp[2] + qs);

            //    if (fp[2] + qs != "Main.aspx" && !SessionBox.User.IsAccessFunc(SessionBox.CurrentSystem, fp[2] + qs, SessionBox.CultrueInfo))
            //    {

            //        Jscript.Redirect(Request.ApplicationPath + "/NonAccess.aspx?Type=NonAccess");
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Response.Redirect("Error.aspx?Msg="+ex.Message);
            //}
            try
            {
                string appPath = Request.ApplicationPath;
                string filePath = Request.FilePath;
                string url = string.Empty;
                if (appPath.Length == 1)
                {
                    url = filePath.Substring(1);
                }
                else
                {
                    url = filePath.Substring(appPath.Length + 1);
                }

                BLL.SysFunctions sysFuncs=new BLL.SysFunctions();
                if(sysFuncs.Exists(url,SessionBox.CurrentSystem)) //Judge whether is it a menu or not, if it's not a menu, it's could be access by eavery one. --DragonXiong temp using this method.
                {
                    if (url != "Main.aspx" && !SessionBox.User.IsAccessFunc(SessionBox.CurrentSystem, url, SessionBox.CultrueInfo))
                    {
                        if (url != "UserLoginLog.aspx" && url.IndexOf("PersonalSetting.aspx") < 0)
                        {
                            if (appPath.Length > 1)
                            {
                                Jscript.Redirect(Request.ApplicationPath + "/NonAccess.aspx?Type=NonAccess");
                            }
                            else
                            {
                                Jscript.Redirect("/NonAccess.aspx?Type=NonAccess");

                            }
                        }
                    }

                }

            }
            catch (Exception ex)
            {
                Response.Redirect("Error.aspx?Msg=" + ex.Message);
            }
        }

        /// <summary>
        /// Check the user online or forced logout,this function can judge the user online or not in time. 
        /// If offline, end the Session.
        /// </summary>
        /// <returns></returns>
        private bool OnlineCheck()
        {

            string strUserId = SessionBox.User.UserID;
            string sessionId = Session.SessionID;
            bool judgeexist = false;

            IDictionary<string, string> list = Application.Get("GLOBAL_USER_LIST") as IDictionary<string, string>;

            if (list != null)
            {
                foreach (KeyValuePair<string, string> keyValue in list)
                {
                    if (strUserId.Equals(keyValue.Key.Trim(), StringComparison.OrdinalIgnoreCase) && sessionId.Equals(keyValue.Value.Trim(), StringComparison.OrdinalIgnoreCase))
                    {
                        judgeexist = true;
                        break;
                    }
                }

                if (!judgeexist)
                {
                    //Response.Write("<script type=\"text/javascript\">alert('You have been logout.Please login again!');</script>");
                    Session.Abandon();
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
